﻿using Cinch;
using FR.SAFTAnalyser.Model.Stocks;
using FR.SAFTAnalyser.ViewModels.Workspace;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FR.SAFTAnalyser.SAFT
{
    public static class CsvSerializer
    {
        public static StockFile Deserialize(string csvFileName)
        {
            StockFile stockFile = new StockFile();
            stockFile.StockHeader = new StockHeader();

            System.IO.StreamReader file = null;
            try
            {
                //store the columns positions
                int category = 0, code = 0, description = 0, numberCode = 0, quantity = 0, unitMeasure = 0;
                //parse the quantity column
                decimal stockQuantity = 0;
                //parse the category column
                ProductCategory stockCategory;

                //open the csv file
                file = new StreamReader(csvFileName);

                List<Stock> stocks = new List<Stock>();

                //read the first line, where it contains the columns names and get the columns positions
                var columns = file.ReadLine().Split(';').ToList();
                category = columns.IndexOf("ProductCategory");
                code = columns.IndexOf("ProductCode");
                description = columns.IndexOf("ProductDescription");
                numberCode = columns.IndexOf("ProductNumberCode");
                quantity = columns.IndexOf("ClosingStockQuantity");
                unitMeasure = columns.IndexOf("UnitOfMeasure");

                //store the current line on a string builder
                StringBuilder line = new StringBuilder();
                while (line.Append(file.ReadLine()).Length > 0)
                {
                    var values = line.ToString().Split(';');
                    Stock stock = new Stock();
                    stock.ProductCode = values[code];
                    stock.ProductDescription = values[description];
                    stock.ProductNumberCode = values[numberCode];
                    stock.UnitOfMeasure = values[unitMeasure];

                    if (Enum.TryParse<ProductCategory>(values[category], out stockCategory))
                        stock.ProductCategory = stockCategory;
                    if (decimal.TryParse(values[quantity], out stockQuantity))
                        stock.ClosingStockQuantity = stockQuantity;

                    stocks.Add(stock);

                    line.Clear();
                }

                //save the stocks to the stock file
                stockFile.Stock = stocks.ToArray();
            }
            catch (Exception ex)
            {
                Mediator.Instance.NotifyColleaguesAsync<string>(MessageType.EXCEPTION_FOUND, ex.Message);
            }
            finally
            {
                if (file != null)
                {
                    file.Close();
                    file.Dispose();
                }
            }

            return stockFile;
        }
    }
}
